#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//ProcParamsOutputs.cpp  - Collection of Output Parameters for Image Processing Steps       
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2012 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Feb 2012
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
#include "tinyxml.h"
#include "ImageExptLib.h"

#include "ImageProcFn.h"

#include "ProcParamInt.h"
#include "ProcParamDouble.h"
#include "ProcParamString.h"
#include "ProcParamDefString.h"

#include "ProcParamsOutputs.h"

const char * const ProcParamsOutputs::xmlOutputs = "Outputs";

double const ProcParamsOutputs::ScoreInvalid = -1.0;


ProcParamsOutputs::ProcParamsOutputs(void)
{
}

ProcParamsOutputs::ProcParamsOutputs(TiXmlElement *stepXml) : ProcParams(stepXml, ProcParamsOutputs::xmlOutputs)
{
}

ProcParamsOutputs::ProcParamsOutputs(const ProcParamsOutputs *params) : ProcParams(params) 
{

}

ProcParamsOutputs::~ProcParamsOutputs(void)
{
	SetError(true);
}

bool	ProcParamsOutputs::SetParamValue(const char *Name, int Value)
{
	bool rc = false;

	if (Name == '\0')
		ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(int)", "ERROR: name is null");
	else
	{
		ProcParamInt *param = dynamic_cast<ProcParamInt *>(GetParam(Name));
		if (param == '\0')
			ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(int)", "ERROR: %s is not an integer param", Name);
		else
		{
			if (IsValid() == false)
				ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(int)", "ERROR: %s, outputs are invalid", Name);
			else
			{
				param->SetValue(Value);
				rc = true;
			}
		}
	}
	return rc;
}

bool	ProcParamsOutputs::SetParamValue(const char *Name, double Value)
{
	bool rc = false;

	if (Name == '\0')
		ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(double)", "ERROR: name is null");
	else
	{
		ProcParamDouble *param = dynamic_cast<ProcParamDouble *>(GetParam(Name));
		if (param == '\0')
			ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(double)", "ERROR: %s is not a double param", Name);
		else
		{
			if (IsValid() == false)
				ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(double)", "ERROR: %s, outputs are invalid", Name);
			else
			{
				param->SetValue(Value);
				rc = true;
			}
		}
	}

	return rc;
}

bool	ProcParamsOutputs::SetParamValue(const char *Name, const char *Value)
{
	bool rc = false;

	if (Name == '\0')
		ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(string)", "ERROR: name is null");
	else
	{
		ProcParamString *param = dynamic_cast<ProcParamString *>(GetParam(Name));
		if (param == '\0')
			ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(string)", "ERROR: %s is not a string param", Name);
		else
		{
			if (IsValid() == false)
				ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(string)", "ERROR: %s, outputs are invalid", Name);
			else
			{
				if ((Value == '\0') || ( strnlen(Value, IPluginICProcStepsParamDefString::MaxLen+1 ) == IPluginICProcStepsParamDefString::MaxLen))
					ImageExptLib::LogMsg("ProcParamsOutputs::SetParamValue(string)", "ERROR: %s, Value is null or len > IPluginICProcStepsParamDefString::MaxLen", Name);
				else
				{
					param->SetValue(Value);
					rc = true;
				}
			}
		}
	}
	return rc;
}

bool	ProcParamsOutputs::SetResultCode(int result)	//set result before score
{
	bool   rc = false;

	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamsOutputs::SetResultCode", "ERROR: outputs are invalid");
	else
	{
		if (result == ProcParamsOutputs::ResultNotSet )	//only set ResultNotSet when initially adding param to param set
			ImageExptLib::LogMsg("ProcParamsOutputs::SetResultCode", "ERROR: result == %d is ProcParamsOutputs::ResultNotSet", ProcParamsOutputs::ResultNotSet);
		else
			rc = SetParamValue(IPluginICProcStepsParamAttribOutputNameResult, result);
	}
	return rc;
}

bool	ProcParamsOutputs::SetScore(double Value)	//only for analysis param set
{
	bool   rc = false;

	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamsOutputs::SetScore", "ERROR: outputs are invalid");
	else
	{
		if (IsResultOK() == false)
			ImageExptLib::LogMsg("ProcParamsOutputs::SetScore", "ERROR: IsResultOK() failed");
		else
		{
			double passScore = GetScore();					//_outputParams->AddParam(new ProcParamDouble(IPluginICProcStepsParamAttribOutputNameScore, 1.0)));	//result is OK if score > 1.0, else NOK
			if ( passScore == ProcParamsOutputs::ScoreInvalid)
				ImageExptLib::LogMsg("ProcParamsOutputs::SetScore", "ERROR: passScore == %s is invalid",  ProcParamsOutputs::ScoreInvalid);
			else
			{
				if ( SetParamValue(IPluginICProcStepsParamAttribOutputNameScore, Value) == false)
					ImageExptLib::LogMsg("ProcParamsOutputs::SetScore", "ERROR: SetParamValue(%g) failed", Value);
				else
				{
					if (Value < passScore)
						SetResultCode(ProcParamsOutputs::ResultScoreFail);
					rc = true;
				}
			}
		}
	}
	return rc;
}

int		ProcParamsOutputs::GetResultCode()
{
	int rc = ProcParamsOutputs::ResultInvalid;	//errors from DLL must be > 0

	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamsOutputs::GetResultCode", "ERROR: outputs are invalid");
	else
	{
		ProcParamInt *result = dynamic_cast<ProcParamInt *>(GetParam(IPluginICProcStepsParamAttribOutputNameResult));
		if (result == '\0' )
			ImageExptLib::LogMsg("ProcParamsOutputs::GetResultCode", "ERROR: GetParam(IPluginICProcStepsParamAttribOutputNameResult) returned null");
		else
			rc = result->GetValue();
	}
	return rc;
}

double	ProcParamsOutputs::GetScore()
{
	double rc = ProcParamsOutputs::ScoreInvalid;

	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamsOutputs::GetScore", "ERROR: outputs are invalid");
	else
	{
		ProcParamDouble *score = dynamic_cast<ProcParamDouble *>(GetParam(IPluginICProcStepsParamAttribOutputNameScore));
		if (score == '\0' )
			ImageExptLib::LogMsg("ProcParamsOutputs::GetScore", "ERROR: GetParam(IPluginICProcStepsParamAttribOutputNameScore) returned null");
		else
			rc = score->GetValue();
	}
	return rc;
}

bool	ProcParamsOutputs::IsResultOK()
{
	bool rc = false;

	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamsOutputs::IsResultOK", "ERROR: outputs are invalid");
	else
	{
		if ( GetResultCode() == ProcParamsOutputs::ResultOK )
			rc = true;
	}
	return rc;
}

bool	ProcParamsOutputs::IsAnalysisSet()
{
	bool rc = false;

	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamsOutputs::IsResultOK", "ERROR: outputs are invalid");
	else
	{
		if ( GetParam(IPluginICProcStepsParamAttribOutputNameScore) != '\0')
			rc = true;
	}

	return rc;
}

bool	ProcParamsOutputs::Validate()
{
	bool rc = false;

	if (ProcParams::Validate())
	{
		if ( GetParam(IPluginICProcStepsParamAttribOutputNameResult) == '\0' )
			ImageExptLib::LogMsg("ProcParamsOutputs:::Validate", "ERROR: IPluginICProcStepsParamAttribOutputNameResult not found");
		else
			rc = true;
	}
	SetError( (rc == true) ? false : true);

	return rc;
}

bool ProcParamsOutputs::IsValid()
{ 
	bool rc = false;

	if (ProcParams::IsValid())
		rc = true;

	return rc;
}


//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------